package org.jdna.minecraft.tools;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

public class PreferencesDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField textField;
	private JCheckBox chckbxCreateBackups;
	private JCheckBox chckbxCreateFullBackups;
	private Preferences prefs;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			PreferencesDialog dialog = new PreferencesDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public PreferencesDialog() {
		this.prefs=Application.getInstance().getPreferences();
		setTitle("Preferences");
		setBounds(100, 100, 640, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));
		{
			JLabel lblMinecraftLocation = new JLabel("Minecraft Location:");
			contentPanel.add(lblMinecraftLocation, "2, 2, right, default");
		}
		{
			textField = new JTextField();
			contentPanel.add(textField, "4, 2, fill, default");
			textField.setColumns(10);

			File dir = prefs.getMinecraftDirAsFile();
			if (dir!=null) {
				textField.setText(dir.getAbsolutePath());
			}
		}
		{
			JButton btnNewButton = new JButton("Choose...");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					File f = Application.getInstance().chooseMinecraftDir();
					if (f!=null) {
						textField.setText(f.getAbsolutePath());
					}
				}
			});
			contentPanel.add(btnNewButton, "6, 2");
		}
		{
			chckbxCreateBackups = new JCheckBox("Create Backups");
			contentPanel.add(chckbxCreateBackups, "4, 4");
			chckbxCreateBackups.setSelected(prefs.isCreateBackupsEnabled());
		}
		{
			chckbxCreateFullBackups = new JCheckBox("Create Full Backups");
			chckbxCreateFullBackups.setToolTipText("If disabled then only the minecraft.jar will be backed up, and not the entire minecraft area");
			chckbxCreateFullBackups.setSelected(prefs.isCreateFullBackupsEnabled());
			contentPanel.add(chckbxCreateFullBackups, "4, 6");
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						try {
							File dir = new File(textField.getText());
							if (!prefs.isValidMinecraftDir(dir)) {
								throw new Exception("Not a valid minecraft dir");
							}
						} catch (Throwable t) {
							Application.getInstance().error("Validation Failed", "Invalid Directory Location: " + textField.getText());
							return;
						}
						
						prefs.setMinecraftDir(textField.getText());
						prefs.setCreateBackupsEnabled(chckbxCreateBackups.isSelected());
						prefs.setCreateFullBackupsEnabled(chckbxCreateFullBackups.isSelected());
						prefs.store();
						setVisible(false);
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						setVisible(false);
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

}
